<?php
namespace app\index\controller;

use think\facade\Config;
use app\common\controller\Base as CommonBase;
use app\common\model\Banner as BannerModel;
use app\common\model\Article as ArticleModel;

class Base extends CommonBase
{
    public function initialize()
    {
        parent::initialize();

        $top_menus = Config::get('app.top_menus');
        $this->assign('top_menus', $top_menus);

        // 默认选中顶部菜单
        $this->assign('active_nav', '');

        $this->current_user = login_user();
        $this->assign('current_user', $this->current_user);

        $BannerModel = new BannerModel();
        $links = $BannerModel->userTypeSelect('link', 0);
        $this->assign('links', $links);
    }

    protected function findValidArticle($id, $opts = []){
        if(isset($opts['view_mode'])){
            $view_mode = $opts['view_mode'];
        }else{
            $view_mode = $this->request->param('mode');
        }
        $view_mode = trim($view_mode);
        $this->assign('view_mode', $view_mode);

        if(isset($opts['valids'])){
            $valids = $opts['valids'];
        }else{
            $valids = [];
        }
        if(isset($opts['tree_node'])){
            $tree_node = $opts['tree_node'];
        }else{
            $tree_node = $this->tree_node;
        }
        if(isset($tree_node['id'])){
            $first_category_id = $tree_node['id'];
        }else{
            $first_category_id = -1;
        }
        $valids['first_category_id'] = $first_category_id;

        $ArticleModel = new ArticleModel();
        $article = $ArticleModel->userFind($id, $view_mode, $valids);
        return $article;
    }
}